// 配置路径别名
var path = require('path')
const CopyWebpackPlugin = require("copy-webpack-plugin")

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  // 配置基础链接地址，根据发布环境添加
  publicPath: process.env.NODE_ENV === 'production'
    ? ''
    : '/',
  outputDir: 'preview',
  devServer: {
    proxy: {
      '/api/v0/': {
        target: 'http://localhost:7001',
        changeOrigin: true,
      },
    }
  },
  productionSourceMap: false,
  lintOnSave: process.env.NODE_ENV !== 'production',
  assetsDir: 'static',
  filenameHashing: true, // 发布打包文件是否有哈希后缀
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src'),
        //'@@': resolve('src/components')
      }
    },
    plugins: [
      new CopyWebpackPlugin([{
        from: "./static",
        to: 'static'
      }])
    ]
  },
  chainWebpack: config => {
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    config.module.rule('md')
      .test(/\.md/)
      .use('vue-loader')
      .loader('vue-loader')
      .end()
      .use('vue-markdown-loader')
      .loader('vue-markdown-loader/lib/markdown-compiler')
      .options({
        raw: true
      })
      .end();
    if (process.env.NODE_ENV === 'production') {
      config.optimization.splitChunks({
        cacheGroups: {
          common: {//commons 一般是是个人定义的
            name: 'chunk-common', // 打包后的文件名
            chunks: 'initial',
            minChunks: 1,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 1,
            reuseExistingChunk: true
          },
          vendors: {//vendor 是导入的 npm 包
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            chunks: 'initial',
            maxSize: 1024 * 1024,
            maxInitialRequests: 20,
            priority: 2,
            reuseExistingChunk: true,
            enforce: true
          },
          echarts: {//把echarts从chunk-vendors.js提取出来
            name: 'chunk-echarts',
            test: /[\\/]node_modules[\\/]echarts[\\/]/,
            chunks: 'initial',
            priority: 3,
            // maxSize: 600000,
            reuseExistingChunk: true,
            enforce: true
          },
          tinymce: {//把tinymce从chunk-vendors.js提取出来
            name: 'chunk-tinymce',
            test: /[\\/]node_modules[\\/]tinymce[\\/]/,
            chunks: 'initial',
            priority: 3,
            // maxSize: 600000,
            reuseExistingChunk: true,
            enforce: true
          },
          xlsx: {//把xlsx从chunk-vendors.js提取出来
            name: 'chunk-xlsx',
            test: /[\\/]node_modules[\\/]xlsx[\\/]/,
            chunks: 'initial',
            priority: 3,
            // maxSize: 600000,
            reuseExistingChunk: true,
            enforce: true
          },
          elementUI: {//把element-ui从chunk-vendors.js提取出来
            name: 'chunk-element-ui',
            test: /[\\/]node_modules[\\/]element-ui[\\/]/,
            chunks: 'initial',
            priority: 3,
            // maxSize: 600000,
            reuseExistingChunk: true,
            enforce: true
          }
        }
      })
    }
  }
}
